package com.wellink.android_demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity2 extends AppCompatActivity {

    private CheckBox cb0 = null;
    private CheckBox cb1 = null;
    private CheckBox cb2 = null;

    private RadioButton rb0 = null;
    private RadioButton rb1 = null;
    private RadioGroup rg = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.checkbox_layout);

        cb0 = findViewById(R.id.cb_0);
        cb1 = findViewById(R.id.cb_1);
        cb2 = findViewById(R.id.cb_2);

        rb0 = findViewById(R.id.rb0);
        rb1 = findViewById(R.id.rb1);
        rg = findViewById(R.id.rg);

        cb0.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                showCheckboxSelect(buttonView);
            }
        });

        cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                showCheckboxSelect(buttonView);
            }
        });

        cb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                showCheckboxSelect(buttonView);
            }
        });

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                String msg = null;
                switch (checkedId){
                    case R.id.rb0:
                        msg = rb0.getText().toString();
                        break;
                    case R.id.rb1:
                        msg = rb1.getText().toString();
                        break;
                        default:
                            break;
                }

                Toast.makeText(MainActivity2.this,"Select:"+msg,Toast.LENGTH_SHORT).show();
            }
        });

    }

    private void showCheckboxSelect(CompoundButton btn){
        String text = btn.getText().toString();

        if(btn.isChecked()){
            Toast.makeText(MainActivity2.this,"check:"+ text,Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(MainActivity2.this,"UnCheck:"+ text,Toast.LENGTH_SHORT).show();
        }
    }
}
